/**
 * File: Student.java
 * Author: Ben Stutsky
 * Team: Bob-omb Squad
 * Date: 4/18/11
 * Email: stutsky1@umbc.edu
 */
package clientApp;

import java.io.Serializable;
import java.util.ArrayList;


/**
 * The Class Student.
 */
public class Student extends User implements Serializable{
	private static final long serialVersionUID = -1307081455327718084L;

	/** The plans. */
	private ArrayList<Plan> plans = new ArrayList<Plan>();
	
	/** The degree. A constant from project constants*/
	private int degree;
	
	public Student(String username, String password) {
		this(username, password, "No Name", ProjectConstants.UNDECIDED, new ArrayList<Plan>());
	}
	
	public Student(String myUsername, String myPassword, String myName, int myDegree)
	{
		this(myUsername, myPassword, myName, myDegree, new ArrayList<Plan>());
	}
	
	/**
	 * Instantiates a new student.
	 *
	 * @param myUsername the my username
	 * @param myPassword the my password
	 * @param myName the my name
	 * @param myDegree the my degree
	 * @param myPlans the my plans
	 */
	public Student(String myUsername, String myPassword, String myName, int myDegree, ArrayList<Plan> myPlans)
	{
		setUsername(new String(myUsername));
		password = new String(myPassword);
		name = new String(myName);
		plans = myPlans;
		degree = myDegree;
	}
	
	public ArrayList<String> getPlanNames()
	{
		ArrayList<String> myPlanNames = new ArrayList<String>();
		for(int i = 0; i != plans.size(); i++)
		{
			myPlanNames.add(plans.get(i).getPlanName());
		}
		return myPlanNames;
	}
	
	public void addPlan(Plan newPlan)
	{
		/* Edited by Rotem to return shallow copy for GUI reference */
		plans.add(newPlan);
	}
	
	public boolean deletePlan(String planName)
	{
		for(int i = 0; i != plans.size(); i++)
		{
			Plan trialPlan = plans.get(i);
			if(planName.compareTo(trialPlan.getPlanName()) == 0)
			{
				plans.remove(i);
				return true;
			}
		}
		return false;
	}
	
	/**
	 * Gets the degree.
	 *
	 * @return the degree
	 */
	public int getDegree() {
		return degree;
	}

	/**
	 * Sets the degree.
	 *
	 * @param degree the new degree
	 */
	public void setDegree(int myDegree) {
		this.degree = myDegree;
	}
	
	/**
	 * Gets the plans.
	 *
	 * @return the plans
	 */
	public ArrayList<Plan> getPlans() 
	{
		ArrayList<Plan> myTemp = new ArrayList<Plan>();
		
		for(int i = 0; i != plans.size(); i++)
		{
			myTemp.add(new Plan(plans.get(i)));
		}
		
		return myTemp;
	}

	/**
	 * Gets the plan with name.
	 *
	 * @param planName the plan name
	 * @return the plan with name
	 */
	public Plan getPlanWithName(String planName)
	{
		for(int i = 0; i != plans.size(); i++)
		{
			Plan trialPlan = plans.get(i);
			if(planName.compareTo(trialPlan.getPlanName()) == 0)
			{
				return new Plan(trialPlan);
			}
		}
		//plan not found
		return null;
	}
	
	private Plan getShallowPlan(String planName)
	{
		for(int i = 0; i != plans.size(); i++)
		{	
			Plan temp = plans.get(i);
			if(temp.getPlanName().compareTo(planName) == 0)
			{
				return temp;
			}
		}
		return null;
	}
	
	public ArrayList<String> getAllSemesterTitlesInPlan(String planName)
	{
		Plan temp = getShallowPlan(planName);
		if(temp == null)
		{
			return null;
		}
		
		return temp.getAllSemesterSeasonSpaceYear();
	}
	 
	public boolean changeCourseRecordGrade(String planName, int season, int year, String courseID, char grade)
	{
		Plan temp = getShallowPlan(planName);
		if(temp == null)
		{
			return false;
		}
		return temp.changeCourseRecordGrade(season, year, courseID, grade);
	}
	
	public boolean changeCourseRecordNotes(String planName, int season, int year, String courseID, String notes)
	{
		Plan temp = getShallowPlan(planName);
		if(temp == null)
		{
			return false;
		}
		return temp.changeCourseRecordNotes(season, year, courseID, notes);
	}
	
	public boolean getSemesterPrevious(String planName, int season, int year)
	{
		Plan temp = getShallowPlan(planName);
		if(temp == null)
		{
			return false;
		}
		return temp.getSemesterIsPrevious(season, year);
	}
	
	public boolean addCourseRecord(String planName, int season, int year, CourseRecord newCR)
	{
		Plan temp = getShallowPlan(planName);
		if(temp == null)
		{
			return false;
		}
		return temp.addCourseRecordToSemester(season, year, newCR);
	}
	
	public boolean removeCourseRecord(String planName, int season, int year, String courseID)
	{
		Plan temp = getShallowPlan(planName);
		if(temp == null)
		{
			return false;
		}
		return temp.removeCourseRecordFromSemester(season, year, courseID);
	}
	
	public ArrayList<String> getCourseIDs(String planName, int season, int year)
	{
		Plan temp = getShallowPlan(planName);
		if(temp == null)
		{
			return null;
		}
		return temp.getCourseIDsFromSemester(season, year);
	}
	 
	public CourseRecord getCourseRecord(String planName, int season, int year, String courseID)
	{
		Plan temp = getShallowPlan(planName);
		if(temp == null)
		{
			return null;
		}
		return temp.getCourseRecordFromSemester(season, year, courseID);
	}
	
	public ArrayList<CourseRecord> getCourseRecords(String planName, int season, int year)
	{
		Plan temp = getShallowPlan(planName);
		if(temp == null)
		{
			return null;
		}
		return temp.getCourseRecordsFromSemester(season, year);
	}
	
	public boolean addSemesterToPlan(String planName, Semester newSem)
	{
		Plan temp = getShallowPlan(planName);
		if(temp == null)
		{
			return false;
		}
		temp.addSemesterToPlan(newSem);
		
		return true;
	}
	
	public boolean removeSemesterFromPlan(String planName, int season, int year)
	{
		Plan temp = getShallowPlan(planName);
		if(temp == null)
		{
			return false;
		}
		temp.deleteSemester(season, year);
		return true;
	}
	
	public boolean changePlanName(String planName, String newName)
	{
		Plan temp = getShallowPlan(planName);
		if(temp == null)
		{
			return false;
		}
		temp.setPlanName(newName);
		return true;
	}
	
	public Semester getSemester(String planName, int season, int year)
	{
		Plan temp = getShallowPlan(planName);
		if(temp == null)
		{
			return null;
		}
		return temp.getSemester(season, year);
	}
	
	public ArrayList<Semester> getSemesters(String planName)
	{
		Plan temp = getShallowPlan(planName);
		if(temp == null)
		{
			return null;
		}
		return temp.getSemesters();
	}
	
	public boolean setSemesterOverridden(String planName, int season, int year, boolean overridden)
	{
		Plan temp = getShallowPlan(planName);
		if(temp == null)
		{
			return false;
		}
		temp.setSemesterOverridden(season, year, overridden);
		return true;
	}

	public String toString()
	{
		String retString = "";
		
		retString += "Username: " + getUsername() + "\n";
		retString += "Password: " + getPassword() + "\n";
		retString += "Name: " + getName() + "\n";
		retString += "Degree: " + ProjectConstants.getDegreeString(getDegree()) + "\n";
		
		for(Plan myPlan : plans)
		{
			retString += myPlan.toString() + "\n";
		}
		
		return retString;
	}
	
	public static void main(String[] args)
	{
		Plan myPlan = new Plan("StutskyPlan");
		
		//Populating the plan
		Semester mySem1 = new Semester(ProjectConstants.FALL, 2009);
		Semester mySem2 = new Semester(ProjectConstants.SPRING, 2010);
		Semester mySem3 = new Semester(ProjectConstants.FALL, 2010);
		Semester mySem4 = new Semester(ProjectConstants.SPRING, 2011);
		
		CourseRecord myCR11 = new CourseRecord("CMSC201", 'A', "This is my 201. Rofl.");
		CourseRecord myCR12 = new CourseRecord("MATH151", 'B', "This is my 201. Rofl.");
		CourseRecord myCR13 = new CourseRecord("HEBR101", 'A', "This is my 201. Rofl.");
		CourseRecord myCR14 = new CourseRecord("ENG100", 'A', "This is my 201. Rofl.");
		
		CourseRecord myCR21 = new CourseRecord("MATH221", 'A', "This is my 201. Rofl.");
		CourseRecord myCR24 = new CourseRecord("CMSC304", 'B', "This is my 201. Rofl.");
		CourseRecord myCR22 = new CourseRecord("CMSC202", 'A', "This is my 202. Rofl.");
		CourseRecord myCR23 = new CourseRecord("CMSC203", 'A', "This is my 203. Rofl.");
		CourseRecord myCR25 = new CourseRecord("HEBR102", 'A', "This is my 201. Rofl.");
		
		CourseRecord myCR31 = new CourseRecord("CMSC313", 'A', "This is my 201. Rofl.");
		CourseRecord myCR32 = new CourseRecord("HEBR201", 'B', "This is my 201. Rofl.");
		CourseRecord myCR33 = new CourseRecord("CHEM101", 'B', "This is my 201. Rofl.");
		CourseRecord myCR34 = new CourseRecord("CMSC341", 'B', "This is my 341. Rofl.");
		
		CourseRecord myCR41 = new CourseRecord("CMSC345", ProjectConstants.NO_GRADE, "This is my 201. Rofl.");
		CourseRecord myCR42 = new CourseRecord("CMSC331", ProjectConstants.NO_GRADE, "This is my 201. Rofl.");
		CourseRecord myCR43 = new CourseRecord("CHEM102", ProjectConstants.NO_GRADE, "This is my 201. Rofl.");
		CourseRecord myCR44 = new CourseRecord("STAT355", ProjectConstants.NO_GRADE, "This is my 201. Rofl.");
		
		mySem1.addCourseRecord(myCR11);
		mySem1.addCourseRecord(myCR12);
		mySem1.addCourseRecord(myCR13);
		mySem1.addCourseRecord(myCR14);
		
		mySem2.addCourseRecord(myCR21);
		mySem2.addCourseRecord(myCR22);
		mySem2.addCourseRecord(myCR23);
		mySem2.addCourseRecord(myCR24);
		mySem2.addCourseRecord(myCR25);
		
		mySem3.addCourseRecord(myCR31);
		mySem3.addCourseRecord(myCR32);
		mySem3.addCourseRecord(myCR33);
		mySem3.addCourseRecord(myCR34);
		
		mySem4.addCourseRecord(myCR41);
		mySem4.addCourseRecord(myCR42);
		mySem4.addCourseRecord(myCR43);
		mySem4.addCourseRecord(myCR44);
		
		myPlan.addSemesterToPlan(mySem1);
		myPlan.addSemesterToPlan(mySem2);
		myPlan.addSemesterToPlan(mySem3);
		myPlan.addSemesterToPlan(mySem4);
		
		Student me = new Student("stutsky1", "PW", "Ben Stutsky", ProjectConstants.CMSC);
		me.addPlan(myPlan);
		System.out.println(me);
		
		System.out.println("\nTest get plan names\n");
		ArrayList<String> myPlanNames = me.getPlanNames();
		for(String myName : myPlanNames)
		{
			System.out.println(myName);
		}
		
		System.out.println("\nTest add plan\n");
		me.addPlan(new Plan("charly plan"));
		myPlanNames = me.getPlanNames();
		for(String myName : myPlanNames)
		{
			System.out.println(myName);
		}
		
		System.out.println("\nTest delete plan\n");
		me.deletePlan("charly plan");
		myPlanNames = me.getPlanNames();
		for(String myName : myPlanNames)
		{
			System.out.println(myName);
		}
		
		System.out.println("\nTest get all semester titles in plans\n");
		ArrayList<String> semTitles = me.getAllSemesterTitlesInPlan("StutskyPlan");
		for(String myName : semTitles)
		{
			System.out.println(myName);
		}
		
		
		
		
		
		
		
		
		
		
	}
}